package icbc.nav.control
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Strong;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BevelFilter;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	
	import icbc.app.AppText;
	import icbc.app.control.Alert;
	import icbc.nav.entity.Menu;
	import icbc.nav.entity.MenuChild;
	
	import org.osflash.signals.Signal;
	
	public class MenuControl extends Sprite
	{
		private const MENU_MARGIN_X:uint = 15;
		public var panel:Sprite;
		public var viewWidth:uint;
		public var viewHeight:uint;
		private var menu:Menu;
		private var menuItemWidth:uint = 310;
		private var menuItemHeight:uint = 41;
		private var initialMenuPosx:int = 0;
		private var initialMenuPosy:int = 0;
		private var menuOpenPosy:int = 41;
		private var currentOpenLevel:uint = 0;
		private var levelStepPosX:Array = [0,5];
		private var levelStepPosXHome:Array = [0,0];
		private var levelStepPosXAccessBanking:Array = [0,0];
		private var menuHome:MenuRootButton;
		private var menuAccessBanking:MenuRootButton;
		public var onShowMenu:Signal = new Signal(int);
		public var onHideMenu:Signal = new Signal();

		public function MenuControl(menu:Menu)
		{
			super();
			this.menu = menu;
			addEventListener(Event.ADDED_TO_STAGE, initStage);
		}

		private function initStage(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initStage);
			initContent();
		}

		private function initContent():void
		{
			buildMenu();
		}

		protected function setBasePanel():Sprite
		{
			viewWidth = 200;
			viewHeight = 400;
			return buildPlainPanel();
		}
		
		protected function buildPlainPanel():Sprite
		{
			var base:Sprite = new Sprite();
			var rounded:uint = 5;
			base.graphics.beginFill(0xFEFEFE);
			base.graphics.drawRoundRect(0, 0, viewWidth, viewHeight, rounded, rounded);
			base.graphics.endFill();
			var filterD:DropShadowFilter = new DropShadowFilter(0, 60, 0x000000, 0.7, 10, 10, 1, 2, false, false, false);
			base.filters = [filterD];
			return base;
		}
		
		private function buildMenu():void
		{
			panel = new Sprite();
			addChild(panel);
			var posx:uint = 0;
			var posy:int = 0;
			var gapx:uint = 10;
			for each(var menuChild:MenuChild in menu.childs)
			{
				var menuWidth:int = menuChild.width > 0 ? menuChild.width : menuItemWidth;

				var item:MenuRootButton = new MenuRootButton(
					{label:menuChild.name, source:menuChild, icon:menuChild.icon}, menuWidth, menuItemHeight);

				item.addEventListener(MenuRootButton.EVENT_CLICK, menuItemClick);
				panel.addChild(item);
				item.x = posx; posx += item.width+gapx;
				item.y = posy;
				if(menuChild.name == AppText.MENU_HOME)
				{
					menuHome = item;
				}
				if(menuChild.name == AppText.MENU_ACCESSBANKING)
				{
					menuAccessBanking = item;
				}
			}
			panel.x = stage.stageWidth/2 - posx/2;
			panel.y = stage.stageHeight/2 - panel.height/2;
			initialMenuPosy = panel.y;
			initialMenuPosx = panel.x;

			levelStepPosX[0] = initialMenuPosx;
			levelStepPosXHome[0] = initialMenuPosx;
			if(menuHome!=null)
			{
				levelStepPosXHome[1] = panel.width/2 - menuHome.x + 10;
			}
			levelStepPosXAccessBanking[0] = initialMenuPosx;
			levelStepPosXAccessBanking[1] = panel.width/2 - menuAccessBanking.x + 10;
		}
		
		private function hideMenuList():void
		{
			menuAccessBanking.visible = true;
			menuAccessBanking.clearState();
			if(menuHome!=null)
			{
				menuHome.visible = true;
				menuHome.clearState();
			}
			var tween:TweenLite = new TweenLite(panel,0.5,{ease:Strong.easeOut,y:initialMenuPosy,x:initialMenuPosx});
			onHideMenu.dispatch();
		}
		
		private function showMenuList(menuButton:MenuRootButton):void
		{
			var posx:int = panel.width/2 - menuButton.x + 20;
			var posy:int = menuOpenPosy + 40;
			var tween:TweenLite = new TweenLite(panel,0.5,{ease:Strong.easeOut,y:posy,x:posx});
		}
		
		private function menuItemClick(evt:Event):void
		{
			var menuButton:MenuRootButton = evt.target as MenuRootButton;
			openMenu(menuButton, []);
		}
		
		private function openMenu(menuButton:MenuRootButton, remainingMenu:Array):void
		{
			var menuChild:MenuChild = menuButton.data.source;
			if(menuChild.childs.length == 0) return;
			showMenuList(menuButton);

			var parentPoint:Point = new Point(menuButton.x, menuOpenPosy);

			var menuList:MenuListControl = new MenuListControl(menuChild, parentPoint, remainingMenu);
			menuList.addEventListener(MenuListControl.EVENT_CLOSE, closeMenuHandler);
			menuList.onOpenLevel.add(openLevelHandler);
			menuList.onCloseLevel.add(closeLevelHandler);
			menuList.onContentOpened.add(contentOpenedHandler);
			panel.addChild(menuList);

			var menuName:String = menuButton.data.source.name;
			if(menuName == AppText.MENU_HOME)
			{
				menuAccessBanking.visible = false;
			}
			if(menuName == AppText.MENU_ACCESSBANKING)
			{
				if(menuHome!=null)
				{
					menuHome.visible = false;
				}
			}
			//openBotMenu(false);
		}
		
		private function openLevelHandler():void
		{
			currentOpenLevel++;
			if(currentOpenLevel>0)
			{
				openBotMenu();
			}
		}
		
		private function openBotMenu(nomove:Boolean = true):void
		{
			var menuButton:MenuRootButton = menuHome != null && menuHome.visible ? menuHome : menuAccessBanking;
			if(nomove)
			{
				var posx:int = levelStepPosX[1] - menuButton.x + MENU_MARGIN_X;
				movePanelX(posx);
			}
			var menuNro:int = menuHome != null && menuHome.visible ? 0 : 1;
			onShowMenu.dispatch(menuNro);
		}

		private function closeLevelHandler():void
		{
			currentOpenLevel--;
			if(currentOpenLevel == 0)
			{
				var posx:int = menuHome != null && menuHome.visible ? levelStepPosXHome[1] : levelStepPosXAccessBanking[1];
				movePanelX(posx);
				onHideMenu.dispatch();
			}
		}
		
		public function contentOpenedHandler(menuChild:MenuChild):void
		{
			if(menuChild.name == AppText.MENU_HOME || menuChild.name == AppText.MENU_ACCESSBANKING)
			{
				if(menuChild.childs.length > 0 && menuChild.childs[0].type == MenuChild.TYPE_CONTENT)
				{
					trace("type:"+ menuChild.type + "."+ menuChild.childs.length);
					openBotMenu();
				}
			}
		}
	
		private function closeMenuHandler(evt:Event):void
		{
			hideMenuList();
			var menuList:MenuListControl = evt.target as MenuListControl;
			menuList.removeEventListener(MenuListControl.EVENT_CLOSE, closeMenuHandler);
			panel.removeChild(menuList);
		}
		
		public function closeChild():void
		{
			currentOpenLevel = 0;
			panel.x = levelStepPosX[currentOpenLevel];
			panel.y = initialMenuPosy;
			for(var i:uint = 0; i<panel.numChildren; i++)
			{
				if(panel.getChildAt(i) is MenuListControl)
				{
					var item:MenuListControl = panel.getChildAt(i) as MenuListControl;
					item.closeControl();
				}
			}
		}
		
		public function navigateTo(menuName:String, fullPath:Array):void
		{
			var firstMenuName:String = fullPath[0];
			for(var i:uint = 0; i<panel.numChildren; i++)
			{
				if(panel.getChildAt(i) is MenuRootButton)
				{
					var item:MenuRootButton = panel.getChildAt(i) as MenuRootButton;
					var menuChild:MenuChild = item.data.source;
					if(menuChild.name == firstMenuName)
					{
						fullPath.reverse();
						fullPath.pop();
						fullPath.reverse();
						openMenu(item, fullPath);
					}
				}
			}
		}
		
		public function navigateToRootMenu(menuName:String):void
		{
			if(menuName == AppText.MENU_HOME && menuHome != null && menuHome.visible)
			{
				return;
			}
			if(menuName == AppText.MENU_ACCESSBANKING && menuAccessBanking.visible)
			{
				return;
			}
			closeChild();
			var menuNav:MenuRootButton = null;
			if(menuName == AppText.MENU_HOME)
			{
				menuNav = menuHome;
			}
			if(menuName == AppText.MENU_ACCESSBANKING)
			{
				menuNav = menuAccessBanking;
			}
			if(menuNav == null) return;
			openMenu(menuNav, []);
		}

		private function movePanelX(posx:int):void
		{
			var tween:TweenLite = new TweenLite(panel,0.5,{ease:Strong.easeOut,x:posx});
		}
	}
}